package casa.depesos.app.com.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.support.toast.ToastUtil;

import casa.depesos.app.com.adapter.BankCardAdapter;
import casa.depesos.app.com.databinding.ActivityBankCardBinding;
import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.NoInfoDlg;
import casa.depesos.app.com.dlg.ReadDlg;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.BankCardBean;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.bean.ProgressBean;
import casa.depesos.app.com.http.data.BankCard;
import casa.depesos.app.com.http.req.BankCardReq;
import casa.depesos.app.com.http.req.DefaultCardReq;
import casa.depesos.app.com.http.req.DelBankCardReq;
import casa.depesos.app.com.http.req.ProgressReq;
import casa.depesos.app.com.http.req.no.CardNumReq;
import casa.depesos.app.com.utils.CommonJump;
import casa.depesos.app.com.utils.Utils;

public class BankCardActivity extends BaseActivity {

    ActivityBankCardBinding binding;
    BankCardAdapter adapter;
    int status;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBankCardBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BankCardAdapter(this, null);
        binding.list.setAdapter(adapter);

        binding.top.title.setText("Gestión de cuenta bancaria");
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.top.back.setOnClickListener(v -> finish());
        new CardNumReq().req();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        getStatus();
    }

    private void getStatus() {
        new ProgressReq().req(new ReqCallback<ProgressBean>() {
            @Override
            public void callback(boolean success, ProgressBean bean) {
                hideLoading();
                if (success && bean.isValid()) {
                    status = bean.data.status;
                }
            }
        });
    }

    private void getData() {
        showLoading();
        new BankCardReq().req(new ReqCallback<BankCardBean>() {
            @Override
            public void callback(boolean success, BankCardBean bean) {
                hideLoading();
                if (success && bean.isValid()) {
                    adapter.setData(bean.card);
                }
            }
        });
    }

    @Override
    protected boolean needPadding() {
        return false;
    }

    public void delete(BankCard card, int p) {
        if (adapter.getItemCount() <= 1) {
            ToastUtil.showLongToast(this, "Conserva al menos una cuenta receptora para recibir pagos");
            return;
        }
        if ("y".equalsIgnoreCase(card.isDefault)) {
            ToastUtil.showLongToast(this, "No se admite eliminar la cuenta receptora predeterminada.");
            return;
        }
        new ReadDlg().setMsg("¿Desea eliminar esta cuenta bancaria?", "Cancelar", "Confirmar")
                .setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                    @Override
                    public void onDismiss(boolean isOk, Object value) {
                        if (isOk) {
                            doDelete(card, p);
                        }
                    }
                }).show(getSupportFragmentManager(), "");
    }

    boolean deleting = false;

    private void doDelete(BankCard card, int p) {
        showLoading();
        if (deleting) {
            return;
        }
        deleting = true;
        new DelBankCardReq(card.id).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                deleting = false;
                if (success) {
                    adapter.delete(p);
                }
            }
        });
    }

    public void add() {
        if (status < 50) {
            new NoInfoDlg().setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        new CommonJump().jump(status, false);
                    }
                }
            }).show(getSupportFragmentManager(), "");
        } else {
            Intent i = new Intent(this, BankActivity.class);
            i.putExtra("from", 1);
            startActivity(i);
        }
    }

    public void selectCard(BankCard card) {
        if (getIntent().getIntExtra("from", 0) == 1) {
            Intent i = new Intent();
            i.putExtra("card", card);
            setResult(RESULT_OK, i);
            finish();
        }
    }

    public void setDefault(BankCard card) {
        if ("y".equalsIgnoreCase(card.isDefault)) {
            return;
        }
        showLoading();
        new DefaultCardReq(card.id).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                if (success) {
                    getData();
                } else {
                    hideLoading();
                }
            }
        });
    }
}
